//******************************************************************/
//版本说明:
//V0.1		2017-03-30	11:00	yshao	复制自SL909_G01_X32
//V2.1		2019-01-31	11:00	yshao	调整init_end和comm_en,init_mode
//V3.0		2019-05-20	23:00	yshao	复制自LL901_G04_X04_11
//V4.0		2019-08-15	23:00	yshao	修改自LL901_G04_X06_25
//******************************************************************/
//******************************************************************/
//			   全局参数定义
//******************************************************************/
//硬件类型
//`define	YS1028
`define	H611
//`define	REDUNDANCE

//仿真模式
//`define	SIMULATION	1
//`timescale	1ps/1ps

//FPGA程序模式
//`define	APP_MODE	1
//`define	TEST_MODE	2
`define	BOOT_MODE	3

//******************************************************************/
//			   FPGA顶层
//******************************************************************/
module LL902_N02(
		//时钟，按键，Led
		input	wire		sclkin,
		input	wire		s_pullup,		
		input	wire		key_in,
		output	wire		led_g,
		
		//调试接口
		output	wire		test_a,
		output	wire		test_b,
		output	wire		test_c,
		output	wire		test_d,
		
		//Flash接口
		output	wire		flash_CS_n,
		output	wire		flash_SCK,
		output	wire		flash_SI,
		input	wire		flash_SO,

		//PHY配置
		output	wire		phy_rst,
		output	wire		phy_mdc,
		inout	tri		phy_mdio,

		//PHY通信
		input	wire		gp0_rxc,
		input	wire		gp0_rxdv,
		input	wire	[3:0]	gp0_rxd,
		output	wire		gp0_txc,
		output	wire		gp0_txen,
		output	wire	[3:0]	gp0_txd,
		
		input	wire		gp1_rxc,
		input	wire		gp1_rxdv,
		input	wire	[3:0]	gp1_rxd,
		output	wire		gp1_txc,
		output	wire		gp1_txen,
		output	wire	[3:0]	gp1_txd,

		//SDRAM接口
		output	wire		sa_csn,
		output	wire		sa_cke,
		output	wire	[3:0]	sa_dqm,
		output	wire		sa_clk,
		output	wire	[2:0]	sa_cnt,
		output	wire	[10:0]	sa_addr,
		output	wire	[1:0]	sa_bank,
		inout	tri	[31:0]	sa_data,

		//SD卡接口
		input	wire		sd_dat1,
		input	wire		sd_wp,
		input	wire		sd_clk,
		input	tri		sd_cmd_sdi,
		output	wire		sd_dat2,
		output	wire		sd_dat0_sdo,
		output	wire		sd_dat3_cs,
		output	wire		sd_cd,

		//MCU接口
		input	wire		mcu_dmx_tx,		//uart2_sync,
		input	wire		mcu_dmx_rx,		//uart3_sync,

		input	wire		mcu_spi_fpga,		//spi工作在通讯模式
		input	wire		mcu_spi_clk,
		input	wire		mcu_spi_mosi,
		input	wire		mcu_spi_miso,

		output	wire		spi2_cs,
		output	wire		spi2_clk,
		output	wire		spi2_mosi,
		output	wire		mcu_fpga_ctrl,		//mcu到fpga的模式控制信号
		
		//灯具控制
		output	wire	[7:0]	port_da,
		output	wire	[7:0]	port_le,
//		inout	tri	[7:0]	port_le,
		output	wire	[5:0]	port_ex,
		input	wire		port_in
		
		//灯具控制
		);

//****************************************************************
//		内部信号
//****************************************************************
//复位&时钟
wire		resetb, oclk, sclk, clk_25M;
wire	[20:0]	time_us, sync_us;
wire	[1:0]	pll_lock, pll_reset;

//PHY接口
wire		sub_mode;
wire	[1:0]	depth_state;
wire	[9:0]   current_depth;
wire	[5:0]	device_port;
wire	[1:0]	device_main;

wire		rec_flag_a, rec_flag, rec_error;
wire	[7:0]	rec_data;
wire	[1:0]	force_send;
wire		send_flag, pre_flag;
wire	[7:0]	send_data;
wire	[1:0]	rec_vendor;
wire		yt_vs_pre, redu_flag, blank_flag, input_L9;
wire	[7:0]	phy_state;

//PHY设置总线相关信号
wire		init_mode, set_d_ok, ext_d_ok;
wire	[31:0]	set_addr;
wire	[7:0]	set_data, rd_data;
wire	[7:0]	mcu_rd_hub;

//MCU设置总线相关信号
wire		mcu_set_d_ok, mcu_ext_d_ok;
wire	[31:0]	mcu_set_addr;
wire	[7:0]	mcu_set_data, mcu_rd_data;

//显示数据相关
wire		vs_L9, ds_L9, h_start_L9, l2048_mode;
wire		ds_a, h_start_a, vs_sd, ds_sd, h_start_sd;
wire	[7:0]	data_L9, data_a, data_sd;
wire	[10:0]	h_num_L9, h_num_a, h_num_sd;

wire	[15:0]	state;
wire	[1:0]	color_restore_type;
wire	[7:0]	testmode;
wire	[8:0]	cascade_light;

reg		vs, ds, h_start;
reg	[7:0]	data;
reg	[10:0]	h_num;

//输出模块接口信号
wire		vs_out;
wire		mem_read_req, mem_data_sync;
wire	[9:0]	mem_pixel_addr;
wire	[31:0]	mem_read_data;	
wire	[7:0]	port_da_o, port_le_o;

//DMX输出信号
wire		dmx_send_flag, mcu_dmx_ten;

//SD播放信号
wire	[7:0]	sd_state;
wire		input_local, force_dis_en, force_send_play;
reg		local_dis_flag;

//SDRAM控制信号
wire	[1:0]	frame_buf_sel;
wire		map_adj_en;

//反馈网口信号
wire		send_flag_sd, pre_flag_sd;
wire	[7:0]	send_data_sd;

//**********MCU接口信号**********
//wire		mcu_dmx_tx, mcu_dmx_rx,	mcu_fpga_ctrl;
//wire		mcu_spi_fpga, mcu_spi_clk, mcu_spi_mosi, mcu_spi_miso;
//wire		spi2_cs, spi2_clk, spi2_mosi;

//**********MCU信号**********
wire		f_flash_CS_n, f_flash_SCK, f_flash_SI, f_flash_SO;
wire		JTRST, JTCK, JTDI, JTMS, JTDO;
wire	[7:0]	mcu_port_a;

wire		w_d_ok, r_flag;
wire	[29:0]	w_addr, r_addr;
wire	[31:0]	w_data, r_data;

//**********PHY复位**********
reg	[4:0]	ms_count;
wire		fpga_phy_rst, fpga_phy_mdc, fpga_mdio_oe, fpga_mdio_o, fpga_mdio_i;
	
//**********调试用信号**********
wire	[15:0]	debug_data, phy_tout;
wire	[31:0]	a1_tout, sa_tout, sd_tout, m_tout, mcu_tout;
wire	[15:0]	out_tout, ms_tout;

//******************************************************************/
//			   参数定义
//******************************************************************/
//程序版本信息
parameter	MAIN_FUNCTION	=  "L";		//ASCII "S"  
parameter	SUB_FUNCTION	=  "L";		//ASCII "L"  
parameter	MAIN_SOLUTION	=  9;		//"9"        
parameter	SUB_SOLUTION	=  2;		//"09"       
parameter	APPLICATION_TYPE=  "N";		//ASCII "G"  
parameter	MAIN_VERSION	=  8'd2;	//"03"       
parameter	SUB_VERSION	=  8'd1;	//"X01"
parameter	MINI_VERSION	=  8'd3;	//" "  

//模块参数设置
defparam	main_ctrl.phy_comm.state_ctrl.main_function	=MAIN_FUNCTION;
defparam	main_ctrl.phy_comm.state_ctrl.sub_function	=SUB_FUNCTION;
defparam	main_ctrl.phy_comm.state_ctrl.main_solution	=MAIN_SOLUTION;
defparam	main_ctrl.phy_comm.state_ctrl.sub_solution	=SUB_SOLUTION;
defparam	main_ctrl.phy_comm.state_ctrl.application_type	=APPLICATION_TYPE;
defparam	main_ctrl.phy_comm.state_ctrl.main_version	=MAIN_VERSION;
defparam	main_ctrl.phy_comm.state_ctrl.sub_version	=SUB_VERSION;
defparam	main_ctrl.phy_comm.state_ctrl.mini_version	=MINI_VERSION;

defparam        main_ctrl.mcu_comm.main_function	=	MAIN_FUNCTION;
defparam        main_ctrl.mcu_comm.sub_function		=	SUB_FUNCTION;
defparam        main_ctrl.mcu_comm.main_solution	=	MAIN_SOLUTION;
defparam        main_ctrl.mcu_comm.sub_solution		=	SUB_SOLUTION;
defparam        main_ctrl.mcu_comm.application_type	=	APPLICATION_TYPE;
defparam        main_ctrl.mcu_comm.main_version		=	MAIN_VERSION;
defparam        main_ctrl.mcu_comm.sub_version		=	SUB_VERSION;
defparam        main_ctrl.mcu_comm.mini_version		=	MINI_VERSION;

//**************************************************************
//			时钟和复位
//**************************************************************
sys_reset_clk sys_reset_clk(
		//输入时钟
		.sclkin(sclkin),

		//输出复位和时钟
		.resetb(resetb),
		.sclk(sclk),
		.oclk(oclk),
		.sa_clk(sa_clk),
		.clk_25M(clk_25M),

		//分频时钟
		.time_us(time_us),
		.sync_us(sync_us),

		//按键检测
		.pll_reset(2'b00),
		.pll_lock(),

		//调试信号	
		.tout()
		);

assign	pll_reset = 2'b00;

//**************************************************************
//			内部MCU
//**************************************************************
a3_mcu_top u_mcu(
		//时钟和复位
		.resetb(resetb),
		.sclk(sclk),
                
		//jtag接口
		.JTRST(1'b1),
		.JTCK(JTCK),
		.JTDI(JTDI),
		.JTMS(JTMS),
		.JTDO(JTDO),

		//外部flash接口
		.flash_CS_n(flash_CS_n),
		.flash_SCK(flash_SCK),
		.flash_SI(flash_SI),
		.flash_SO(flash_SO),

		//内部总线
		.w_d_ok		(w_d_ok),
    		.w_addr		(w_addr),
		.w_data		(w_data),
    		.r_flag		(r_flag),
   		.r_addr		(r_addr),
    		.r_data		(r_data),

		//GPIO
		.mcu_io_a	(),
		.mcu_in_b	(8'h0),
		.mcu_out_c	(),

		//USART
		.UART_TXD(),
		.UART_RXD(1'b1),

		//PHY初始化
		.phy_rst	(phy_rst),
		.phy_mdc	(phy_mdc),
		.phy_mdio	(phy_mdio),

		//FPGA模块Flash控制
		.fpga_f_CS_n	(fpga_f_CS_n),
		.fpga_f_SCK	(fpga_f_SCK),
		.fpga_f_SI	(fpga_f_SI),
		.fpga_f_SO	(fpga_f_SO),

		//FPGA模块Flash控制
	        .fpga_phy_rst	(fpga_phy_rst),
	        .fpga_phy_mdc	(fpga_phy_mdc),
	        .fpga_mdio_oe	(fpga_mdio_oe),
	        .fpga_mdio_o	(fpga_mdio_o),
	        .fpga_mdio_i	(fpga_mdio_i),

		//调试信号
		.tout(mcu_tout)
		);

assign	JTRST = sd_dat1;
assign	JTDI = sd_wp;
assign	JTMS = sd_clk;
assign	JTCK = sd_cmd_sdi;
assign	sd_dat2 = JTDO;

//**************************************************************
//		   PHY初始化
//**************************************************************    
always @(posedge sclk or negedge resetb)
	if (resetb == 0)
		ms_count <= 0;
	else if (init_mode == 1)
		ms_count <= 1;
	else if ((ms_count[4] == 0) && (sync_us[10] == 1))
		ms_count <= ms_count + 1;

assign	fpga_phy_rst = ms_count[4];

AR8035_top u_AR8035_top(
		.sclk(sclk),
		
		.mii_mode(mii_mode),
		
		.cfg_end(cfg_end),
		
		.rst_n(fpga_phy_rst),
		.mdc(fpga_phy_mdc),
		.mdio_oe(fpga_mdio_oe),
		.mdio_o(fpga_mdio_o),
		.mdio_i(fpga_mdio_i)
		);
		
//**************************************************************
//			通讯模块
//**************************************************************
phy_interface phy_interface(
		.resetb(resetb),
		.sclk(sclk),
                
		.sync_16ms(sync_us[14]),

		//其他外部相关信号
		.sub_mode(sub_mode),
		.cas_depth_adj(depth_state[1]),
		.local_depth(current_depth[7:0]),

		.tx_err_en(1'b0),

		//PORTA接口
		.gp0_rxc(gp0_rxc),
		.gp0_rxdv(gp0_rxdv),
		.gp0_rxd(gp0_rxd),
		.gp0_txc(gp0_txc),
		.gp0_txen(gp0_txen),
		.gp0_txd(gp0_txd),
		
		//PORTB接口
		.gp1_rxc(gp1_rxc),
		.gp1_rxdv(gp1_rxdv),
		.gp1_rxd(gp1_rxd),
		.gp1_txc(gp1_txc),
		.gp1_txen(gp1_txen),
		.gp1_txd(gp1_txd),
		
		//内部的接收接口
		.rec_flag_a(rec_flag_a),
		.rec_flag(rec_flag),
		.rec_data(rec_data),
		.rec_error(rec_error),
		.rec_error_sync(),
		
		//内部的发送接口
		.force_send(force_send),
		.send_flag(send_flag),
		.pre_flag(pre_flag),
		.send_data(send_data),

		//给内部的其他信号
		.input_port(input_port),
		.rec_vendor(rec_vendor),
		.yt_vs_pre(yt_vs_pre),
		.input_active(input_L9),
		.redu_flag(redu_flag),
		.blank_flag(blank_flag),
		.phy_state(phy_state),
		
		.tout(phy_tout)
		);
		
//**************************************************************
//			FPGA主控逻辑
//**************************************************************
main_ctrl main_ctrl(
		//复位时钟，按键，Led
		.resetb(resetb),
		.sclk(sclk),

		.time_us(time_us),
		.sync_us(sync_us),
		
		//按键和指示灯
		.key_in(1'b0),
		.led_g(led_g),

		//Flash接口
		.flash_CS_n(f_flash_CS_n),
		.flash_SCK(f_flash_SCK),
		.flash_SI(f_flash_SI),
		.flash_SO(f_flash_SO),

		//PHY配置接口
		.phy_rst(),
		.phy_mdc(),
		.phy_mdio(),

		//冗余控制信号
		.device_main(device_main),

                //外部MCU通讯接口
		.mcu_fpga_ctrl(1'b0),
		
		.mcu_spi_fpga(1'b0),
		.mcu_spi_clk(1'b0),
		.mcu_spi_mosi(1'b0),
		.mcu_spi_miso(),

		.spi2_cs(),
		.spi2_clk(),
		.spi2_mosi(),

		//PHY通讯接口
		.sub_mode(sub_mode),
		.depth_state(depth_state),
		.current_depth(current_depth),

		.map_adj_en(map_adj_en),
		.input_L9(input_L9),
		.phy_state(phy_state),
		.redu_flag(redu_flag),
		.blank_flag(blank_flag),
		.rec_vendor(rec_vendor),
		
		.rec_flag_a(rec_flag_a),
		.rec_flag(rec_flag),
		.rec_data(rec_data),
		.rec_error(rec_error),
		.rec_error_sync(),
		
		.force_send(force_send),
		.send_flag(send_flag),
		.pre_flag(pre_flag),
		.send_data(send_data),
		
		//PHY总线接口
		.init_mode(init_mode),
		.set_d_ok(set_d_ok),
		.ext_d_ok(ext_d_ok),
		.set_addr(set_addr),
		.set_data(set_data),
		.rd_data(rd_data),
		.debug_data(debug_data),

		//MCU总线接口
		.mcu_set_d_ok(mcu_set_d_ok),
		.mcu_ext_d_ok(mcu_ext_d_ok),
		.mcu_set_addr(mcu_set_addr),
		.mcu_set_data(mcu_set_data),
		.mcu_rd_data(mcu_rd_hub),

		//L9显示数据接口
		.vs_L9(vs_L9),
		.ds_L9(ds_L9),
		.data_L9(data_L9),
		.h_start_L9(h_start_L9),
		.h_num_L9(h_num_L9),
		.l2048_mode(l2048_mode),

		.state(state),
		.color_restore_type(color_restore_type),
		.testmode(testmode),
		.cascade_light(cascade_light),

		//Artnet显示数据接口
		.input_artnet(input_artnet),
		.ds_a(ds_a),
		.data_a(data_a),
		.h_start_a(h_start_a),
		.h_num_a(h_num_a),

		//背板控制接口
		.device_port(device_port),
		.fpga_dmx_send(dmx_send_flag),
		.mcu_dmx_ten(mcu_dmx_ten),
		
		//SD卡MCU控制接口
		.mcu_sd_en(mcu_sd_en),
		.mcu_sd_miso(mcu_sd_miso),
		
		//SD发送数据包接口
		.force_send_play(force_send_play),
		.send_flag_dis(send_flag_sd),
		.pre_flag_dis(pre_flag_sd),
		.send_data_dis(send_data_sd),

		//SD卡状态
		.sd_state(sd_state),
		
		//调试接口
		.tout(m_tout)   
		);

	
//************************************************************/
//		调试接口
//************************************************************/                    
assign	debug_data = sd_tout[31:16];

reg		debug_output;

always	@(posedge sclk or negedge resetb)
	if(resetb==0)
		debug_output <= 0;
	else if ((mcu_ext_d_ok == 1) && (mcu_set_addr == 32'h0001_2116)) begin
		if (mcu_set_data == 8'h3C)
			debug_output <= 1;
		else
			debug_output <= 0;
		end

assign	port_da = mcu_tout[7:0];
assign	port_le = mcu_tout[15:8];

//assign  port_da = {flash_CS_n, flash_SCK, flash_SI, flash_SO, JTCK, JTDI, JTMS, JTDO};
//assign  port_da = {flash_SI, flash_SO, JTCK, JTDI, JTMS, JTDO};
//assign  port_le = mcu_tout[7:0];

assign  spi2_cs = 0;
assign  spi2_clk = 0;
assign  spi2_mosi = 0;
assign  mcu_fpga_ctrl = 0;

//assign	sd_dat1 = 1'b0;
//assign	sd_wp = 1'b0;
//assign	sd_clk = 1'b0;
//assign	sd_cmd_sdi = 1'b0;
//assign	sd_dat2 = 1'b0;
assign	sd_dat0_sdo = 1'b0;
assign	sd_cd = 1'b0;
assign	sd_dat3_cs = 1'b0;

assign	test_a = 0;
assign	test_b = 0;
assign	test_c = 0;
assign	test_d = 0;

endmodule